package com.yuandengta.yomall.member.controller;

import java.util.Arrays;
import java.util.Map;

import com.yuandengta.common.exception.BizCode;
import com.yuandengta.yomall.member.exception.MemberException;
import com.yuandengta.yomall.member.feign.CouponFeignService;
import com.yuandengta.yomall.member.vo.MemberLoginVO;
import com.yuandengta.yomall.member.vo.MemberRegisterVO;
import com.yuandengta.yomall.member.vo.WeiboUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.web.bind.annotation.*;

import com.yuandengta.yomall.member.entity.MemberEntity;
import com.yuandengta.yomall.member.service.MemberService;
import com.yuandengta.common.utils.PageUtils;
import com.yuandengta.common.utils.R;



/**
 * 会员
 *
 * @author kevin
 * @email 178676392@qq.com
 * @date 2020-12-22 15:44:34
 */
@RestController
@RequestMapping("member/member")
public class MemberController {
    @Autowired
    private MemberService memberService;

    /**
     * 引入远程调用的 Feign 接口
     */
    @Autowired
    private CouponFeignService couponFeignService;

    /**
     * 测试 Feign远程调用
     * @return
     */
    @RequestMapping("/testCouponFeign")
    public R testCouponFeign () {
        return couponFeignService.info(1L);
    }

    /**
     * 微博登录
     * @param weiboUser
     * @return
     */
    @PostMapping("/oauth/weibo/login")
    public R weiboLogin(@RequestBody WeiboUser weiboUser){
        MemberEntity memberEntity = memberService.login(weiboUser);
        if (memberEntity == null) {
            return R.error(BizCode.ACCOUNT_PASSWORD_EXCEPTION.getCode(),
                    BizCode.ACCOUNT_PASSWORD_EXCEPTION.getMessage());
        }
        return R.ok().put("data", memberEntity);
    }

    /**
     * 会员账号密码登录
     * @param vo
     * @return
     */
    @PostMapping("/login")
    public R login(@RequestBody MemberLoginVO vo){
        MemberEntity memberEntity = memberService.login(vo);
        if (memberEntity == null) {
            return R.error(BizCode.ACCOUNT_PASSWORD_EXCEPTION.getCode(),
                    BizCode.ACCOUNT_PASSWORD_EXCEPTION.getMessage());
        }
        return R.ok().put("data", memberEntity);
    }

    /**
     * 会员注册
     * @param registerVO
     * @return
     */
    @PostMapping("/reg")
    public R register(@RequestBody MemberRegisterVO registerVO){
        try {
            memberService.register(registerVO);
        } catch (MemberException e) {
            return R.error(e.getCode(), e.getMessage());
        }

        return R.ok();
    }

    /**
     * 用户唯一性检查
     * @param username
     * @return
     */
    @GetMapping("/check/user/{username}")
    public R checkUser(@PathVariable("username") String username){
        try {
            memberService.checkUsernameUnique(username);
        } catch (MemberException e) {
            return R.error(e.getCode(), e.getMessage());
        }
        return R.ok();
    }

    /**
     * 手机号唯一性检查
     * @param mobile
     * @return
     */
    @GetMapping("/check/mobile/{mobile}")
    public R checkMobile(@PathVariable("mobile") String mobile){
        try {
            memberService.checkMobileUnique(mobile);
        } catch (MemberException e) {
            return R.error(e.getCode(), e.getMessage());
        }
        return R.ok();
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = memberService.queryPage(params);

        return R.ok().put("page", page);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id){
		MemberEntity member = memberService.getById(id);

        return R.ok().put("member", member);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody MemberEntity member){
		memberService.save(member);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody MemberEntity member){
		memberService.updateById(member);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids){
		memberService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
